import Image

img = Image.open('mozart.gif')
im = img.load()

newimg = Image.new('RGB',img.size)
newim = newimg.load()


newline = [0]*640
for y in range(480):
    line = [im[x,y] for x in range(640)]
    # get new line
    # find 195*5
    count = 0
    pre = None
    for index,p in enumerate(line):
        if pre==p:
            count +=1
        else:
            count = 1
        if count>4:
            # copy to newline
            for i in range(640):
                x2 = (index+i)%640
                newline[i] = line[x2]
            break
        pre = p
        
    # copy to newimg
    for x1 in range(640):
        newim[x1,y] = newline[x1]

#for jj in range(480):
#    print newim[1,jj],newim[2,jj];
newimg.save('16.gif')
